package com.chookss.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class AppStatusManager implements Application.ActivityLifecycleCallbacks{
    public int appStatus = AppStatus.STATUS_RECYVLE;    //APP状态 初始值为不在前台状态

    private static AppStatusManager appStatusManager;
    private Application application;
    //是否前台
    private boolean isForground;
    //Activity运行个数
    private int activeCount;

    private AppStatusManager(){}
    private AppStatusManager(Application application){
        this.application = application;
        application.registerActivityLifecycleCallbacks(this);
    }

    public static void init(Application application){
        if(appStatusManager == null){
            appStatusManager = new AppStatusManager(application);
        }
    }

    //单例模式
    public static AppStatusManager getInstance() {
        return appStatusManager;
    }

    public int getAppStatus() {
        return appStatus;
    }

    public void setAppStatus(int appStatus) {
        this.appStatus = appStatus;
    }
    /**
     * 是否前台显示
     * @return
     */
    public boolean isForground(){
        return isForground;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        activeCount++;
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        isForground = true;
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        activeCount--;
        if(activeCount == 0){
            isForground = false;

    }
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }
}
